<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Game</title>
        <link rel="stylesheet" href="../gameStyle.css" type="text/css" />
        <script src="../lib/Box2dWeb-2.1.a.3.js"></script>
        <script type="text/javascript" src="../lib/jquery-latest.js"></script>
        <script type="text/javascript" src="../lib/ivank.js"></script>
        <script src="types.js"></script>
        <!--
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
        <script type="text/javascript" src="http://lib.ivank.net/ivank.js"></script>
        -->
        <script type="text/javascript" src="types.js"></script>
        <script type="text/javascript" src="button.js"></script>
        <script type="text/javascript" src="timer.js"></script>
        <script type="text/javascript" src="level.js"></script>

    </head>
    <body onload="Start();">

        <canvas id="canvas" width="740" height="500"></canvas>
        <script> 
            var world;
            var balls = [];
            var actors = [];
            var canvas = document.getElementById("canvas");
            var cwidth = canvas.getAttribute("width");
            var cheight = canvas.getAttribute("height");
            const RADIUS = 20;
            
            function Start(){
                var stage = new Stage("canvas");
                stage.addEventListener(Event.ENTER_FRAME, update);
                canvas.addEventListener(MouseEvent.CLICK, click, false);
                
                
                var    b2Vec2 = Box2D.Common.Math.b2Vec2
                ,  b2AABB = Box2D.Collision.b2AABB
                ,  b2BodyDef = Box2D.Dynamics.b2BodyDef
                ,  b2Body = Box2D.Dynamics.b2Body
                ,  b2FixtureDef = Box2D.Dynamics.b2FixtureDef
                ,  b2Fixture = Box2D.Dynamics.b2Fixture
                ,  b2World = Box2D.Dynamics.b2World
                ,  b2MassData = Box2D.Collision.Shapes.b2MassData
                ,  b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
                ,  b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
                ,  b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef
                ,  b2DebugDraw = Box2D.Dynamics.b2DebugDraw
                ,  b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef;
                
                world = new b2World(new b2Vec2(0, 10),  true);
                
                var wallBitmap = new Bitmap(new BitmapData("../pictures/steel.jpg"));
                
                function makeStaticBox(x,y,width,height){
                    var fixtureDef	= new b2FixtureDef();	
                    fixtureDef.shape	= new b2PolygonShape();
                    fixtureDef.density =  1.0;
                    fixtureDef.density =  0.5;
                    fixtureDef.density =  0.8;
                    var halfWidth = width/2;
                    var halfHeight = height/2;
                    fixtureDef.shape.SetAsBox(halfWidth, halfHeight);
                    
                    var bodyDef = new b2BodyDef();
                    bodyDef.type = b2Body.b2_staticBody;
                    
                    bodyDef.position.Set(x + halfWidth, y + halfHeight); // stred kocky
                    
                    var box = world.CreateBody(bodyDef);
                    box.CreateFixture(fixtureDef);
                    
                    var wall = new Sprite();
                    wall.addChild(wallBitmap);
                    wall.x = x;
                    wall.y = y;
                    wall.scaleX = width/ 400;
                    wall.scaleY = height/ 322;
                    stage.addChild(wall);
                    
                    return box;
                }
                
                // CREATE WALLS 
                const HRUBKA = 5;
                var walls = new Box(0,0,cwidth,cheight);
                walls.makeBox(HRUBKA);
                
                const boxWidth = 200;
                const boxHeight = 200;
                const boxY = cheight - boxHeight - 100;
                const space = (cwidth - 3*boxWidth)/4;
                var boxA = new Box(space,boxY,boxWidth,boxHeight);
                var boxB = new Box(2*space+boxWidth,boxY,boxWidth,boxHeight);
                var boxC = new Box(3*space+2*boxWidth,boxY,boxWidth,boxHeight);
                
                boxA.makeBox(HRUBKA);
                boxB.makeBox(HRUBKA);
                boxC.makeBox(HRUBKA);
                
                function Box(x,y,w,h){
                    this.x = x;
                    this.y = y;
                    this.width = w;
                    this.height = h;
                    this.makeBox = makeBox;
                    this.isIn = isIn;
                    function makeBox(hrubka){
                        var x = this.x;
                        var y = this.y;
                        var w = this.width;
                        var h = this.height;
                        // floor
                        makeStaticBox(x,y + h - hrubka,w,hrubka);
                        // left wall
                        makeStaticBox(x,y,hrubka,h);
                        // right wall
                        makeStaticBox(x + w - hrubka,y,hrubka,h);
                    }    
                    
                    function isIn(x,y){
                        var w = this.width;
                        var h = this.height; 
                        var result = x > this.x;
                        result &= x < (this.x + w);
                        result &= y > this.y;
                        result &= y < (this.y + h);
                        return result;
                    }
                }
                
               
                // CLICK event
                var ballBitmap = new Bitmap(new BitmapData("../pictures/pBall.png"));
                ballBitmap.x = -(163/2);
                ballBitmap.y = -(164/2);
                function click(event){
                    var x = event.pageX;
                    var y = event.pageY;
                    var blFixDef	= new b2FixtureDef();
                    blFixDef.shape	= new b2CircleShape();
                    blFixDef.shape.SetRadius(RADIUS);
                    blFixDef.density = 1;
                    var bodyDef = new b2BodyDef();
                    bodyDef.type = b2Body.b2_dynamicBody;
                    bodyDef.position.Set(x,y);
                    
                    var ball = world.CreateBody(bodyDef);
                    ball.CreateFixture(blFixDef);
                    balls.push(ball);
                    
                    var actor = new Sprite();
                    actor.addChild(ballBitmap);
                    actor.x = x;// - RADIUS;
                    actor.y = y;// - RADIUS;
                    actor.scaleX = 2*RADIUS/ 163;
                    actor.scaleY = 2*RADIUS/ 164;
                    stage.addChild(actor);
                    actors.push(actor);
                    
                    console.debug(boxA.isIn(x,y));
                }
                
                function update(e) {
                    world.Step(1/10,1,1);
                    world.ClearForces();
                    moveBalls();
                    console.debug(isSolved());
                }
            
                function moveBalls(){
                    for(var i=0; i < actors.length; i++){
                        var ball  = balls[i];
                        var actor = actors[i];
                        var p = ball.GetPosition();
                        actor.rotation = ball.GetAngle()*180/Math.PI;
                        actor.x = p.x;
                        actor.y = p.y;
                    }
                }
                
                function ballsInBox(box){
                    var count = 0;
                    for(var i = 0;i < balls.length;i++){
                        var p = balls[i].GetPosition();
                        if(box.isIn(p.x,p.y)){
                            count++;
                        }
                    }
                    return count;
                }
                
                function isSolved(){
                    var a = ballsInBox(boxA);
                    var b = ballsInBox(boxB);
                    var c = ballsInBox(boxC);
                    return (c*c == (a*a + b*b) ) && (c != 0);
                }
                
            }
            
            /*
            var    b2Vec2 = Box2D.Common.Math.b2Vec2
            ,  b2AABB = Box2D.Collision.b2AABB
            ,  b2BodyDef = Box2D.Dynamics.b2BodyDef
            ,  b2Body = Box2D.Dynamics.b2Body
            ,  b2FixtureDef = Box2D.Dynamics.b2FixtureDef
            ,  b2Fixture = Box2D.Dynamics.b2Fixture
            ,  b2World = Box2D.Dynamics.b2World
            ,  b2MassData = Box2D.Collision.Shapes.b2MassData
            ,  b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
            ,  b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
            ,  b2EdgeChainDef = Box2D.Collision.Shapes.b2EdgeChainDef
            ,  b2DebugDraw = Box2D.Dynamics.b2DebugDraw
            ,  b2MouseJointDef =  Box2D.Dynamics.Joints.b2MouseJointDef;
            
            const canvasId = "canvas";
            var canvas = document.getElementById(canvasId);
            var canvasContext = canvas.getContext();
            var world = new b2World(new b2Vec2(0,500), true);
            var cwidth = canvas.getAttribute("width");
            var cheight = canvas.getAttribute("height");
            
            
            
            function Start(){
                var stage = new Stage("canvas");
                var level = new PythagorasLevel("Pythagoras",8,typeMath);
                
                level.canvas = canvas;
                level.canvasContext = canvasContext;
                level.world = world;
                level.init();
                
                 function update(e) {
                    level.run(e);
                    world.Step(1 / 60, 10, 10);
                    //world.DrawDebugData();
                    world.ClearForces();
                };
                
                stage.addChild(level,0); // pridavam level na index 0, index tam inak nemusim davat
                intervalID = window.setInterval(update, 1000 / 60);  
            }
            
            function makeStaticBoxGlobal(x,y,width,height,world){
                var bodyDef = new b2BodyDef;
                bodyDef.type = b2Body.b2_staticBody;
                bodyDef.position.Set(x, y); // stred kocky
            
                var fixtureDef = new b2FixtureDef;
                fixtureDef.density = 10.0;
                fixtureDef.friction = 0.5;
                fixtureDef.restitution =0.8;
                fixtureDef.shape = new b2PolygonShape;
                fixtureDef.shape.SetAsBox(width, height);
                    
                var box = world.CreateBody(bodyDef);
                box.CreateFixture(fixtureDef);
                return box;
            }
            
            function Box(world){
                this.world = world;
                this.init = init;
                this.height;
                this.width;
                this.setBounds = setBounds;
                this.makeBody = makeBody;
                this.shape;
                this.x;
                this.y;
                const HRUBKA = 5;
                
                function init(){
                    
                }
                
                function setBounds(width, height){
                    this.width = width;
                    this.height = height;
                }
                
                function makeBody(x, y) {
                    this.x = x;
                    this.y = y;
                    var w = this.width/2;
                    var h = this.height/2;
                    const t = HRUBKA/2;
                    makeStaticBoxGlobal(x, y, t, h, this.world);
                    makeStaticBoxGlobal(x + w, y + h - t, w, t, this.world);
                    makeStaticBoxGlobal(x + w*2, y, t, h, this.world);
                }
                
            }
            
            
            
            
            function PythagorasLevel(level, type){
                Sprite.apply(this); // dedim stage  
                Level.apply(this); // dedim level
                
                this.name = name;
                this.level = level;
                this.type = type;
                this.canvas;
                this.canvasContext;
                this.world;
                this.shapes = new Array();
                this.boxA;
                this.boxB;
                this.boxC;
                // methodes
                this.makeStaticBox = makeStaticBox;
                this.makeWalls = makeWalls;
                this.makeABCbox = makeABCbox;
                this.setDebugDraw = setDebugDraw;
                this.init = init;
                this.click = click;
                this.makeDynamicBall = makeDynamicBall;
                //////////////////////////////////////
                
                
                
                function init(){
                    this.makeWalls(5/2);
                    this.makeABCbox();
                    
                    this.setDebugDraw()
                }
                
                function makeWalls(hrubka){
                    //lava stena
                    this.makeStaticBox(hrubka,0,hrubka,canvas.height);
                    // prava stena
                    this.makeStaticBox(canvas.width - hrubka,0,hrubka,canvas.height);
                    // horna stena
                    this.makeStaticBox(0,hrubka,canvas.width,hrubka);
                    // dolna stena
                    this.makeStaticBox(0,canvas.height - hrubka,canvas.width,hrubka);
                }
                
                function setDebugDraw(){
                    var debugDraw = new b2DebugDraw();
                    debugDraw.SetSprite( canvasContext );
                    debugDraw.SetDrawScale(1.0);
                    debugDraw.SetFillAlpha(0.5);
                    //debugDraw.SetLineThickness(1.0);
                    debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
                    world.SetDebugDraw(debugDraw);
                }
                
                function makeStaticBox(x,y,w,h){
                    this.shapes[this.shapes.length] = makeStaticBoxGlobal(x, y, w, h, this.world);
                }
                
                function makeABCbox(){
                    const boxSize = 150;
                    const half = boxSize/5;
                    this.boxA = new Box(world);
                    this.boxA.setBounds(boxSize,boxSize);
                    this.boxA.makeBody(cwidth/3 - boxSize - half,cheight - boxSize - 5);
           
                    this.boxB = new Box(world);
                    this.boxB.setBounds(boxSize,boxSize);
                    this.boxB.makeBody(2*cwidth/3 - boxSize -half,cheight - boxSize - 5);
            
                    this.boxC = new Box(world);
                    this.boxC.setBounds(boxSize,boxSize);
                    this.boxC.makeBody(cwidth - boxSize - half,cheight - boxSize - 5);
            
                }
                
                function makeDynamicBall(x,y,radius){
                    var bodyDef = new b2BodyDef;
                    bodyDef.type = b2Body.b2_dynamicBody;
                    bodyDef.position.Set(x, y); // stred lopty
            
                    var fixtureDef = new b2FixtureDef;
                    fixtureDef.density = 10.0;
                    fixtureDef.friction = 0.5;
                    fixtureDef.restitution =0.8;
                    fixtureDef.shape = new b2CircleShape(radius);
                    
                    var ball = this.world.CreateBody(bodyDef);
                    ball.CreateFixture(fixtureDef);
                    this.shapes[this.shapes.length] = ball;
                }
                
                
                function click(x,y){
                    this.makeDynamicBall(x,y,10);
                }
                
            }
            PythagorasLevel.prototype = new Sprite(); // toto musi byt  
            // PythagorasLevel.prototype = new Level();
            /*
            
            function click(event) {
                var x = event.pageX - canvas.offsetLeft;
                var y = event.pageY - canvas.offsetTop;
                level.click(x,y);
            }*/

           
            
            // window.setInterval(update, 1000 / 60);
            
        </script>

        <script type="text/javascript">
            /*
            jQuery(document).ready(function(){
                $("#canvas").click(function(e){
                    click(e);
                }); 
            })
             */
        </script>
        <script type="text/javascript" src="level.js"></script>

    </body>
</html>
